package com.ruoyi.erecord.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erecord.device.domain.DeviceImportDetail;
import com.ruoyi.erecord.device.domain.bo.DeviceImportDetailBo;
import com.ruoyi.erecord.device.domain.vo.DeviceImportDetailVo;
import com.ruoyi.erecord.device.mapper.DeviceImportDetailMapper;
import com.ruoyi.erecord.device.service.IDeviceImportDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 设备Service业务层处理
 *
 * @author cc
 * @date 2023-04-19
 */
@RequiredArgsConstructor
@Service
public class DeviceImportDetailServiceImpl implements IDeviceImportDetailService {

    private final DeviceImportDetailMapper baseMapper;

    /**
     * 查询设备
     */
    @Override
    public DeviceImportDetailVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询设备列表
     */
    @Override
    public TableDataInfo<DeviceImportDetailVo> queryPageList(DeviceImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DeviceImportDetail> lqw = buildQueryWrapper(bo);
        Page<DeviceImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询设备列表
     */
    @Override
    public List<DeviceImportDetailVo> queryList(DeviceImportDetailBo bo) {
        LambdaQueryWrapper<DeviceImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DeviceImportDetail> buildQueryWrapper(DeviceImportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DeviceImportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), DeviceImportDetail::getBatchNo, bo.getBatchNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), DeviceImportDetail::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSimCode()), DeviceImportDetail::getSimCode, bo.getSimCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DeviceImportDetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getBrand()), DeviceImportDetail::getBrand, bo.getBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), DeviceImportDetail::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), DeviceImportDetail::getName, bo.getName());
        //lqw.orderByDesc(DeviceImportDetail::getCreateTime);
        return lqw;
    }

    /**
     * 新增设备
     */
    @Override
    public Boolean insertByBo(DeviceImportDetailBo bo) {
        DeviceImportDetail add = BeanUtil.toBean(bo, DeviceImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备
     */
    @Override
    public Boolean updateByBo(DeviceImportDetailBo bo) {
        DeviceImportDetail update = BeanUtil.toBean(bo, DeviceImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceImportDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean batchInsertBo(List<DeviceImportDetailVo> voList) {
        List<DeviceImportDetail> list = BeanCopyUtils.copyList(voList, DeviceImportDetail.class);
        return baseMapper.insertBatch(list);
    }
}
